Repository navigation
rtsdiff circular: keep only the posterior branch rule, replace innovation_fn with circular - #242
Merged
Merged
Conversation
…tion_fn with circular #241 fixed #240 by choosing each measurement's 2pi branch relative to the previous posterior, but kept the old prediction-branch rule as circular_branch='prediction' and kept innovation_fn alongside a new circular flag, which forced mutual-exclusion checks and long docstrings. The prediction rule is never better on notebook 7's data at any gain and is the cause of #240, and the only innovation_fn ever used was the angle wrap, so this drops both, makes the branch choice two lines in kalman_filter, and replaces the six #241 tests with one regression test using the issue's own example. Output is identical to master's default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…it also runs with forwardbackward, which the issue noted didn't fix the aliasing Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #241, opened for discussion before merging. #241 fixed #240 by choosing each measurement's 2π branch relative to the previous posterior rather than the prediction. That's the right fix, and this PR keeps it. What it removes is the machinery for keeping the old prediction-branch rule around, because I couldn't find a case where that rule is better.
Changes
kalman_filter:innovation_fnis replaced bycircular. The onlyinnovation_fnwe ever used was the angle wrap, and that's exactly the rule that causes rtsdiff(circular=True) can runaway on an aliased solution #240. This is a breaking change to a public argument that shipped in v0.3 (and is named in the JOSS paper, which describes v0.3), so it should go in the release notes.innovation -= 2π·round((yₙ − C x̂ₙ₋₁|ₙ₋₁)/2π). It shifts the measurement by whole turns to lie within π of the previous posterior. Missing data needs no special handling, because after a gap the previous posterior is the prediction. This also drops theValueErrorfor combiningcircularwithinnovation_fn.rtsdiff:circular_branchis removed, along with its validation and the**wrappingdict. Docstrings are shortened to match, andcircularno longer claims the innovation is wrapped to [−π, π].log_qr_ratio12) is now a two-line check inside the existingtest_circular_rtsdiff, so it runs with and withoutforwardbackward. It fails under the old rule both ways.The output is identical to master's default
'posterior'rule on 72 combinations: a step, a noisy spin, NaN gaps, and 2D input, at orders 1–3,log_qr_ratio2, 8 and 12, with and withoutforwardbackward.Why the #241 tests can go
test_circular_step_no_aliasing(8 cases)test_circular_fly_trajectorytest_circular_matches_unwrapped(uniform, irregular, missing, 2D)test_missing_datacoversrtsdiff's NaN path withoutcircular.test_circular_invariant_to_whole_turnsround(...)by exactly k, so the innovation is unchanged.test_circular_noise_robustness(20 seeds)test_circular_argument_errorscircular_branchparametrization oftest_circular_rtsdiffIs there a reason to keep
'prediction'?#241 kept it on the grounds that it tracks fast, noisy rotation better at low gain. I couldn't find evidence of that:
Notebook 7 (order 1,
log_qr_ratio4): the two rules agree to within 1e-9 on 50 of 50 noise draws, so the notebook's saved outputs stay valid and it needs no rerun. At that gain the prediction never overshoots by more than π, so both rules pick the same branch at every sample.A gain sweep on notebook 7's data, showing the median dxdt RMSE over 10 noise seeds:
log_qr_ratioThe posterior rule is never worse. Where the two rules differ at low gain, the posterior rule is better. At high gain the prediction rule aliases, while the posterior rule only follows the noise, as any high-gain filter would.
🤖 Written by Claude Code on behalf of @pavelkomarov